#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

#
# get data from Metalib
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Decode_Month Add_Delta_YM Add_Delta_Days Delta_Days);

use avsCommon;

#use re 'debug';

my ($content, $response, $request, $url, $session);
my ($runyear, $runmonth, $runday, @rundate);

dbsetup();

my $ua = new LWP::UserAgent(keep_alive=>1);

doLogin();

do {

  @rundate = @begdate;
  
	while (Delta_Days(@rundate, @enddate) > 0) {
		getStats ();
		@rundate = Add_Delta_YM(@rundate, 0, 1);	# advance the month
	};

} while $sthlogin->fetch;

doLogout();
dbshutdown();

# login to metalib admin module
sub doLogin {
	# get the session
	$url = 'http://search3.linccweb.org:8331/M';
	$response = $ua->request(GET $url);

	if (!$response->is_success) {
		die "#error:$0: GET session id $url" . $response->status_line;
	};

	# get the sid from the response
	$content=$response->decoded_content;
	$content =~ /name\=\"identify\" action\=\"http:\/\/search3.linccweb.org:8331\/M\/(?<session>.*?)\"/s;
	$session = $+{session};

	#login the session
	$url = "http://search3.linccweb.org:8331/M/$session/login";
	$response = $ua->request(POST $url,
		[	'P01'		=> $vendorusername,
			'P02' 	=> $vendorpassword ] ) ;

	if (!$response->is_success) {
		die "#error:$0: POST login $url" . $response->status_line;
	};

	$content=$response->decoded_content;
}

# uses vendorId, collegeid, user, pass. retrieves stats and updates db
sub getStats {
	# ask for the report 

	my ($runyear, $runmonth, $runday) = @rundate;
	my ($tmpyear, $tmpmonth, $tmpday) = Add_Delta_Days(Add_Delta_YM($runyear, $runmonth, $runday, 0, 1), -1);	# find last day of month

	$url = 'http://search3.linccweb.org:8331/aleph-cgi/statistics.xls?session_id=' . $session . '&query_name=statistics_02&debug=1&date_type=dmy&from_date_nice=' .
		sprintf('%02d/%02d/%04d', $runday, $runmonth, $runyear) . '&from_date=' . sprintf('%04d%02d%02d', @rundate) . '&to_date_nice=' .
		sprintf('%02d/%02d/%04d', $tmpday, $runmonth, $runyear) . '&to_date=' . sprintf('%04d%02d%02d', $runyear, $runmonth, $tmpday) .
		'&institute=FLCC' . sprintf('%02d', $collegeid) . '00&user_group=&filter_x=A&group_by=base&search_type=&search_type=&report_format=save_as';

	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET data $url" . $response->status_line;
	};

	# here is the statistical data
	$content=$response->decoded_content;

	# save contents for possible debugging
	savecontent('Metalib', $collegeid, $content);

	my $period = myperiod($runyear, $runmonth);

	# split out the actual data lines
	my @contents = split ("\n", $content);
	# sort them ascending by datasource. metalib occasionally sends multiple stats for a datasource
	@contents = sort {substr($a,21,30) cmp substr($b,21,30)} @contents[6..$#contents - 1];	
	
	my $datasourcesave = '';
	my $searchessave = 0;

	foreach $content (@contents) {
		if (length($content)>50) {
			my @dataparts = split ("\t", $content);
			my $datasource = $dataparts[1];
			$datasource =~ s/\s+$//; # rtrim
			my $searches = $dataparts[2];
			$searches =~ s/^\s+//; # ltrim 

			#metalib occasionally sends multiple stats for a datasource, must sum them together
			if ($datasource eq $datasourcesave) {
				$searchessave += $searches;

			} else {
				if ($datasourcesave ne '') {
					print "$vendorid $collegeid $period $datasourcesave 0 $searchessave\n";
					$sthstats->execute($vendorid, $collegeid, $datasourcesave, $period, $searchessave, $REQUESTS)
						or die "#error: sthstats 1 execute failed, check end of log";
				}
				$datasourcesave = $datasource;
				$searchessave = $searches;
			}
		}
	}
	
	# save the last one
	print "$vendorid $collegeid $period $datasourcesave 0 $searchessave\n";
	$sthstats->execute($vendorid, $collegeid, $datasourcesave, $period, $searchessave, $REQUESTS)
		or die "#error: sthstats 1 execute failed, check end of log";

}

sub doLogout {
# logout the session
	$url = "http://search3.linccweb.org:8331/M/$session/logout";
	$response = $ua->request(GET $url);
};
